package com.aier.user.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.user.entity.CallRoll;

public class CallRollRowMapper implements RowMapper<CallRoll> {

    private int type;
    
    public static final int AND_COUNT = 1;

    public CallRollRowMapper() {
    }

    public CallRollRowMapper(int type) {
        this.type = type;
    }

    @Override
    public CallRoll mapRow(ResultSet rs, int rowNum) throws SQLException {
        
        CallRoll cr = parseHome(new CallRoll(), rs);
        switch (type) {
            case AND_COUNT:
                cr.setLeav(rs.getInt("leav"));
                cr.setAbsent(rs.getInt("absent"));
                break;
        }
        
        return cr;
    }

    public static CallRoll parseHome(CallRoll h, ResultSet rs) throws SQLException {
        return parseHome(h, rs, null);
    }

    public static CallRoll parseHome(CallRoll h, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            h.setCrid(rs.getLong(su+"crid"));
        } catch (SQLException e) {
            h.setCrid(null);
        }
        try {
            h.setCid(rs.getLong(su+"cid"));
        } catch (SQLException e) {
            h.setCid(null);
        }
        try {
            h.setCsid(rs.getLong(su+"csid"));
        } catch (SQLException e) {
            h.setCsid(null);
        }
        try {
            h.setState(rs.getInt(su+"state"));
        } catch (SQLException e) {
            h.setState(null);
        }
        try {
            h.setCdate(rs.getString(su+"cdate"));
        } catch (SQLException e) {
            h.setCdate(null);
        }
        try {
            h.setSname(rs.getString(su+"sname"));
        } catch (SQLException e) {
            h.setSname(null);
        }
        return h;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, CallRoll h) {
        par.put("crid",h.getCrid());
        par.put("cid",h.getCid());
        par.put("csid",h.getCsid());
        par.put("state",h.getState());
        par.put("cdate",h.getCdate());
        par.put("sname",h.getSname());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, CallRoll h) {
        args.put("crid",h.getCrid() );
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final CallRoll h) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(h),
                        new String[] { "crid" });
                parsePS(ps, h);
                return ps;
            }

        };
    }

    private static String parseSQL(CallRoll h) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into call_roll(");
        int count = 0;
        if (null != h.getCrid()) {
            sql.append("crid,");
            count++;
        }
        if (null != h.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null != h.getCsid()) {
            sql.append("csid,");
            count++;
        }
        
        if (null != h.getState()) {
            sql.append("state,");
            count++;
        }
        if (null != h.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, CallRoll h)
        throws SQLException {
        int count = 1;
        if (null != h.getCrid()) {
            ps.setLong(count++,h.getCrid());
        }
        if (null != h.getCid()) {
            ps.setLong(count++,h.getCid());
        }
        if (null != h.getCsid()) {
            ps.setLong(count++,h.getCsid());
        }
        if (null != h.getState()) {
            ps.setInt(count++,h.getState());
        }
        if (null != h.getCdate()) {
            ps.setString(count++,h.getCdate());
        }
    }

}
